//
//  registerViewController.m
//  哎游
//
//  Created by 林程杰 on 15/1/13.
//  Copyright (c) 2015年 lanou3g.com 蓝鸥科技. All rights reserved.
//

#import "registerViewController.h"
#import "WebColor.h"
#import "Masonry.h"
#import "sinaWiboViewController.h"
#import "UserDataBase.h"

@implementation registerViewController


- (void)dealloc
{
    [_fieldMessage release];
    [_fieldPas release];
    [_fieldPas2 release];
    [super dealloc];
}

- (instancetype) initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        
    }
    return self;
}

- (void) viewDidLoad
{
    [super viewDidLoad];
    [self.view setBackgroundColor:[WebColor whiteSmoke]];
    self.navigationController.navigationBar.hidden = NO;
    self.navigationController.navigationBar.translucent = NO;
    
    UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 30, 30)];
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:button];
    [button setBackgroundImage:[UIImage imageNamed:@"registerBack.png"] forState:UIControlStateNormal];
    [button addTarget:self action:@selector(back :) forControlEvents:UIControlEventTouchUpInside];
    
    self.navigationItem.title = @"注册";
    
    [button release];
    [self creatView];
}

- (void) creatView
{
    UILabel *labelBack = [[UILabel alloc] init];
     labelBack.font = [UIFont fontWithName:@"TimesNewRomanPS-BoldMT" size:40];
    labelBack.text = @"哎游";
    labelBack.textColor = [WebColor seaGreen];
    [self.view addSubview:labelBack];
    [labelBack mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self.view.mas_centerX).offset(0);
        make.top.equalTo(self.view.mas_top).offset(self.view.frame.size.height / 26);
        make.size.mas_equalTo(CGSizeMake(80, 60));
    }];
    
    
    self.fieldMessage = [[UITextField alloc] initWithFrame:CGRectMake(25, self.view.frame.size.height / 6, self.view.frame.size.width - 50, 40)];
    _fieldMessage.clearsOnBeginEditing = YES;
    _fieldMessage.placeholder = @"输入用户名或你的名字就行啦!";
    _fieldMessage.borderStyle = UITextBorderStyleRoundedRect;
    [self.view addSubview:_fieldMessage];

    self.fieldPas = [[UITextField alloc] init];
    
    _fieldPas.clearsOnBeginEditing = YES;
    _fieldPas.placeholder = @"密码别是你的生日啊~";
    _fieldPas.borderStyle = UITextBorderStyleRoundedRect;
    _fieldPas.secureTextEntry = YES;
    [self.view addSubview:_fieldPas];
    [_fieldPas mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view.mas_left).offset(25);
        make.top.equalTo(_fieldMessage.mas_bottom).offset(40);
        make.size.mas_equalTo(CGSizeMake(self.view.frame.size.width - 50, 40));
    }];
    
    
    self.fieldPas2 = [[UITextField alloc] init];
    
    _fieldPas2.clearsOnBeginEditing = YES;
    _fieldPas2.placeholder = @"确认密码,密码确认";
    _fieldPas2.borderStyle = UITextBorderStyleRoundedRect;
    _fieldPas2.secureTextEntry = YES;
    [self.view addSubview:_fieldPas2];
    [_fieldPas2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view.mas_left).offset(25);
        make.top.equalTo(_fieldPas.mas_bottom).offset(40);
        make.size.mas_equalTo(CGSizeMake(self.view.frame.size.width - 50, 40));
    }];

    UIButton *button = [[UIButton alloc] init];
    [button setBackgroundColor:[WebColor seaGreen]];
    [button setTitle:@"注册点这儿" forState:UIControlStateNormal];
    [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [button setTitleColor:[UIColor grayColor] forState:UIControlStateHighlighted];
    [self.view addSubview:button];
    [button mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view.mas_left).offset(25);
        make.top.equalTo(_fieldPas2.mas_bottom).offset(45);
        make.size.mas_equalTo(CGSizeMake(self.view.frame.size.width - 50, 40));
    }];
    [button addTarget:self action:@selector(registerSuccess) forControlEvents:UIControlEventTouchUpInside];

   
    
    [labelBack release];
    [button release];
    
}

- (void) back : (id) sender
{
//    [self.navigationController popToRootViewControllerAnimated:YES];
//    self.navigationController.navigationBar.hidden = YES;
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    [_fieldMessage resignFirstResponder];
    [_fieldPas resignFirstResponder];
    [_fieldPas2 resignFirstResponder];
}

- (void) registerSuccess
{
    BOOL isExist = [UserDataBase selectForUserName:_fieldMessage.text];
    if (isExist == YES) {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"小提示" message:@"账号已被注册" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        [alert show];
        [alert release];
    }else {
        if (_fieldMessage.text.length == 0 || _fieldMessage.text == nil) {
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"小提示" message:@"请输入账号" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
            [alert show];
            [alert release];
        }else if (_fieldPas.text.length == 0 || _fieldPas.text == nil){
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"小提示" message:@"请输入密码" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
            [alert show];
            [alert release];
        }else if(_fieldPas2.text.length == 0 || _fieldPas2.text == nil){
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"小提示" message:@"请确认密码" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
            [alert show];
            [alert release];
        }else if([_fieldPas.text isEqualToString:_fieldPas2.text] && !(_fieldPas.text.length == 0) && !(_fieldPas.text.length == 0)) {
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"小提示" message:@"注册成功" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
            [self dismissViewControllerAnimated:YES completion:nil];
            
            [UserDataBase insertIntoUserName:_fieldMessage.text passWord:_fieldPas.text];
            
            [alert show];
            [alert release];
        }else{
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"小提示" message:@"确认的密码和原密码不符!" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
            [alert show];
            [alert release];
        }
    }
    
    
}

@end
